package com.army.study.slice;

import com.army.study.ResourceTable;
import com.army.study.utils.CommonData;
import com.army.study.utils.LogUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

import ohos.agp.components.*;


public class MainAbilitySlice extends AbilitySlice {

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_game_home_layout);

        initView();
    }

    private void initView() {
        int count = 4;
        int deviceWidth = getContext().getResourceManager().getDeviceCapability().width;
        int btnHeight = (deviceWidth - (count-1) * 30) / count;

        TableLayout.LayoutConfig tlc1 = new TableLayout.LayoutConfig(0, vp2px(btnHeight));
        tlc1.columnSpec = TableLayout.specification(TableLayout.DEFAULT, 1, 1.0f);
        tlc1.rowSpec = TableLayout.specification(TableLayout.DEFAULT, 1);
        tlc1.setMargins(vp2px(10),vp2px(10),vp2px(10),vp2px(10));

        findComponentById(ResourceTable.Id_btn3).setLayoutConfig(tlc1);
        findComponentById(ResourceTable.Id_btn4).setLayoutConfig(tlc1);
        findComponentById(ResourceTable.Id_btn5).setLayoutConfig(tlc1);

        findComponentById(ResourceTable.Id_btn6).setLayoutConfig(tlc1);
        findComponentById(ResourceTable.Id_btn7).setLayoutConfig(tlc1);
        findComponentById(ResourceTable.Id_btn8).setLayoutConfig(tlc1);
        findComponentById(ResourceTable.Id_btn9).setLayoutConfig(tlc1);

        setBtnListener();
    }

    // 监听表格内所有按钮的事件
    private void setBtnListener() {
        TableLayout table = (TableLayout)findComponentById(ResourceTable.Id_tabelHome);
        int childNum = table.getChildCount();
        for (int index = 0; index < childNum; index++) {
            Button child = (Button)(table.getComponentAt(index));
            child.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    if (component instanceof Button) {
                        Button button = (Button)component;
                        String currentNum = button.getHint();
                        LogUtil.info(MainAbilitySlice.class.getName(), currentNum);
                        // 跳转到游戏界面
                        Intent intent = new Intent();
                        Operation operation = new Intent.OperationBuilder()
                                .withBundleName(getBundleName())
                                .withAbilityName(CommonData.ABILITY_MAIN)
                                .withAction(CommonData.PLAY_PAGE).build();
                        intent.setOperation(operation);
                        intent.setParam(CommonData.KEY_COUNT, Integer.valueOf(currentNum));
                        startAbility(intent);
                    }
                }
            });
        }
    }

    private int vp2px(float vp) {
        return AttrHelper.vp2px(vp, getContext());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
